<?php

namespace App\Kuafu\Pay\Model\Clickhouse;

use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\CustomPayment\Model\CustomPaymentModel;
use App\Kuafu\Order\Model\OrderPayUnionSplitModel;
use App\Kuafu\Pay\Enum\CustomPayEnum;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Pay\Model\RefundStreamModel;
use App\Kuafu\Pay\Model\ShopPaymentModel;
use App\Kuafu\Shop\Model\Shop;
use Kuafu\LaravelClickHouse\Database\Eloquent\Model;

class PayStreamCHModel extends Model
{
    protected $connection   = 'clickhouseOrderAll';
    protected $table        = 'kf_pay_stream';
    protected $primaryKey   = 'payNo';
    protected $keyType      = 'string';

    public function shop()
    {
        return $this->hasOne(Shop::class, 'id', 'shopId');
    }

    public function getPayChannelNameAttribute()
    {
        return PayStreamModel::PAY_CHANNEL_TEXT[$this->payChannel] ?? '';
    }

    public function getPayChannelChildTextAttribute()
    {
        return PayStreamModel::PAY_CHANNEL_CHILD_TEXT[$this->payChannelChild] ?? '';
    }

    // 对应多个退款记录
    public function refundStreamRel()
    {
        return $this->hasMany(RefundStreamModel::class, 'payNo', 'payNo')
                    ->where('refundStatus', '=', RefundStreamModel::REFUND_STATUS_OK);
    }

    /**
     * 结算金额[虚拟属性]
     * 收款 - 手续费
     * @return int 单位：人民币分
     */
    public function getSettlementPriceAttribute(): int
    {
        return $this->payPrice - $this->fee;
    }

    public function getPayChannelSettlementPeriodAttribute()
    {
        $className = PayStreamModel::PAY_CHANNEL_CLASS[$this->payChannel] ?? null;
        if (class_exists($className)) {
            if (defined("$className::PAYMENT_SETTLEMENT_PERIOD")) {
                return $className::PAYMENT_SETTLEMENT_PERIOD;
            }
        }

        // CustomPay
        if ((int)$this->payChannel >= PayStreamModel::PAY_CHANNEL_CUSTOM_PAYMENT) {
            return CustomPayEnum::PAYMENT_SETTLEMENT_PERIOD;
        }

        return '';
    }

    protected static $shopPayments = null;

    public function getPayChannelMerchantIdAttribute()
    {
        if ($this->payChannel == PayStreamModel::PAY_CHANNEL_FY) {
            if (null === static::$shopPayments) {
                static::$shopPayments = [];
                ShopPaymentModel::query()
                                ->where('payChannel', PayStreamModel::PAY_CHANNEL_FY)
                                ->orderBy('id', 'asc')
                                ->chunk(100, function ($models) {
                                    foreach ($models as $model) {
                                        $arr = json_decode($model->payConfig, true);
                                        if (isset($arr['mch_id']) && strlen($arr['mch_id'])) {
                                            static::$shopPayments[$model->shopId] = $arr['mch_id'];
                                        }
                                    }
                                });
            }
            if (isset(static::$shopPayments[$this->shopId])) {
                return static::$shopPayments[$this->shopId];
            }
        }
        return '';
    }

    public function getPayActionTextAttribute()
    {
        return PayStreamModel::PAY_ACTIONS[$this->payAction] ?? '';
    }

    public function streamCustomPaymentRel()
    {
        return $this->hasOne(CustomPaymentModel::class, 'id', 'payChannel');
    }
}
